package com.namecard.namecardapi.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.namecard.namecardapi.entity.CustomTagInfo;
import com.namecard.namecardapi.entity.DTO.TagDTO;
import com.namecard.namecardapi.entity.TagInfo;
import com.namecard.namecardapi.entity.vo.CustomTagVO;
import com.namecard.namecardapi.entity.vo.ResponseVO;
import com.namecard.namecardapi.entity.vo.TagVO;
import com.namecard.namecardapi.entity.vo.UserAllTags;
import com.namecard.namecardapi.service.CustomTagService;
import com.namecard.namecardapi.service.TagInfoService;
import com.namecard.namecardapi.util.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zxb
 */
@RestController
@RequestMapping("cards/tag")
@Api(value = "标签数据处理接口", tags = "标签数据处理接口")
@Slf4j
public class TagController {
    @Resource
    private TagInfoService tagInfoService;

    @Autowired
    CustomTagService customTagService;

    /**
     * 添加自定义二级标签
     */
    @PostMapping("userDefinedTag")
    @ApiOperation("用户自定义二级标签")
    @ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")
    public ResponseVO<Object> userDefinedTag(@RequestBody TagDTO tagDTO, HttpServletRequest request) {
        try {
            String token = request.getHeaders("Authorization").nextElement();
            String userId = TokenUtil.getUserId(token);
            Boolean insert = tagInfoService.userDefinedTag(tagDTO,userId);
            if (insert) {
                return ResponseVO.getSuccessResponseVo("用户自定义二级标签成功");
            }
            return ResponseVO.getSuccessResponseVo("用户自定义二级标签失败");
        } catch (Exception e) {
            log.error("报错如下：{}", e.getMessage());
            throw e;
        }
    }
    /**
     * 更新和新增标签
     */
    @PostMapping("addAndUpdate")
    @ApiOperation("更新和新增标签接口")
    @ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")
    public ResponseVO<Object> createTag(@RequestBody List<TagVO> tagVOList, HttpServletRequest request) {
        try {
            String token = request.getHeaders("Authorization").nextElement();
            String userId = TokenUtil.getUserId(token);
            List<String> tagTitles = tagInfoService.createTag(tagVOList, userId);
            return ResponseVO.getSuccessResponseVo(tagTitles);
        } catch (Exception e) {
            log.error("报错如下：{}", e.getMessage());
            throw e;
        }
    }

    /**
     * 获取所有标签
     */
    @GetMapping("getAllTags")
    @ApiOperation("获取所有标签接口")
    @ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")
    public ResponseVO<Object> getAllTags(HttpServletRequest request) {
        try {
            String token = request.getHeaders("Authorization").nextElement();
            String userId = TokenUtil.getUserId(token);
            //获取所有标签
            List<TagInfo> allTag = tagInfoService.getAllTags();
            return ResponseVO.getSuccessResponseVo(allTag);
        } catch (Exception e) {
            log.error("报错如下：{}", e.getMessage());
            throw e;
        }
    }

    /**
     * 获取用户标签
     */
    @GetMapping("getUserAllTag")
    @ApiOperation("获取用户标签接口")
    @ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")
    public ResponseVO<Object> getUserAllTag(HttpServletRequest request) {
        try {
            String token = request.getHeaders("Authorization").nextElement();
            String userId = TokenUtil.getUserId(token);

            UserAllTags userAllTags = new UserAllTags();
            userAllTags.setUserTags(tagInfoService.getUserAllTags(userId));
            List<CustomTagInfo> customTagInfos = customTagService.list(new QueryWrapper<CustomTagInfo>()
                    .eq("user_id", userId));
            List<String> List = customTagInfos.stream()
                    .map(CustomTagInfo::getTitle)
                    .collect(Collectors.toList());
            userAllTags.setCustomTags(List);

            return ResponseVO.getSuccessResponseVo(userAllTags);
        } catch (Exception e) {
            log.error("报错如下：{}", e.getMessage());
            throw e;
        }
    }

    /**
     * 添加自定义标签接口
     */
    @PostMapping("addUserCustomTag")
    @ApiOperation("添加自定义标签接口")
    @ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")
    public ResponseVO<Object> addUserCustomTag(@RequestBody CustomTagVO customTagVO, HttpServletRequest request) {
    try {
            String token = request.getHeaders("Authorization").nextElement();
            String userId = TokenUtil.getUserId(token);
            CustomTagInfo customTagInfo = new CustomTagInfo(null, userId,customTagVO.getTitle());
            if(customTagService.getOne(new QueryWrapper<CustomTagInfo>()
                    .eq("user_id", userId)
                    .eq("title", customTagVO.getTitle())) != null){
                return ResponseVO.getErrorResponseMsg("自定义标签已存在，添加失败");
        }
            customTagService.save(customTagInfo);
            return ResponseVO.getSuccessResponseVo("成功添加");
        } catch (Exception e) {
            log.error("报错如下：{}", e.getMessage());
            throw e;
        }
    }

    /**
     * 删除自定义标签接口
     */
    @PostMapping("delUserCustomTag")
    @ApiOperation("删除自定义标签接口")
    @ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")
    public ResponseVO<Object> delUserCustomTag(@RequestBody CustomTagVO customTagVO, HttpServletRequest request){
        try {
            String token = request.getHeaders("Authorization").nextElement();
            String userId = TokenUtil.getUserId(token);
            customTagService.remove(new QueryWrapper<CustomTagInfo>()
                    .eq("user_id", userId)
                    .eq("title", customTagVO.getTitle()));
        return ResponseVO.getSuccessResponseVo("成功删除");
    } catch (Exception e) {
            log.error("报错如下：{}", e.getMessage());
            throw e;
        }
    }
}
